<?php

class Core_Component_Login_Controller_Backend_Index {

    public function indexAction() {
        return $GLOBALS[RC]->getHelper()->templateResult();
    }

    public function indexPostAction($username, $password) {
        $m = $GLOBALS[RC]->getMessager();
        $h = $GLOBALS[RC]->getHelper();
        $account = App::getModel('account/account')->loadBy(array('user_name'=>$username));
        if (!$account->getIdValue() || $account->getPassword() != md5($password)) {
            $m->addError('{message.login_error}');
            return $GLOBALS[RC]->getHelper()->redirectResult('*/login/index/index');
        }

        if ($account->getStatus() != CONSTANT_STATUS_ENABLED) {
            $m->addError('{message.disabled_account}');
            return $GLOBALS[RC]->getHelper()->redirectResult('*/login/index/index');
        }

        $identity = $GLOBALS[RC]->getIdentity();
        $identity->setId($account->getId())
                ->setName($account->getUserName());
        if (!$identity->getIsSuper()) {
            $identity->addAccount($account->getChildAccounts()->getItemPropertyValue('user_name'));
            foreach ($account->getRoleCodes() as $role) {
                $identity->addRole($role);
            }
            foreach ($account->getActionCodes() as $code) {
                $identity->addAction($code);
            }
        }

        $identity->login();
        return $GLOBALS[RC]->getHelper()->redirectResult('*/index/index/index');
    }   
}